package com.njcb.ams.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.njcb.ams.interaction.BusinessWrokflowManager;
import com.njcb.ams.orm.dao.ProjectInfoDAO;
import com.njcb.ams.orm.entity.ProjectInfo;
import com.njcb.ams.orm.entity.ProjectInfoExample;
import com.njcb.ams.pojo.dto.ProjectInfoQueryRequest;
import com.njcb.ams.pojo.dto.ProjectInfoQueryResponse;
import com.njcb.ams.portal.comm.DataBus;
import com.njcb.ams.util.AmsBeanUtils;
import com.njcb.ams.util.RetCode;

@Service
public class ProjectInfoService {
    private static final Logger logger = LoggerFactory
	    .getLogger(ProjectInfoService.class);
    @Autowired
    private ProjectInfoDAO projectInfoDAO;

    @Autowired
    private BusinessWrokflowManager businessworkManager;

    /**
     * 方法功能描述：需求信息查询
     * 
     * @param request
     * @return
     */
    public List<ProjectInfoQueryResponse> projectInfoQuery(
	    ProjectInfoQueryRequest request) {
	// 查询数据库
	List<ProjectInfo> list = projectInfoDAO
		.selectByExample(new ProjectInfoExample());
	logger.info("查询得到数据条数{}", list.size());

	String loginName = DataBus.getLoginName();
	List<ProjectInfoQueryResponse> rtlist = new ArrayList<ProjectInfoQueryResponse>();
	// 将实体对象ProjectInfo转为视图对象ProjectInfoQueryResponse
	for (ProjectInfo projectInfo : list) {

	    // 判断是否为当前用户处理
	    String currentHandler = projectInfo.getCurrentHandler();

	    // 如果当前用户字段为空，默认为当前用户处理
	    if (StringUtils.isEmpty(currentHandler)) {
		projectInfo.setCurrentHandler(loginName);
		ProjectInfoQueryResponse app = new ProjectInfoQueryResponse();
		AmsBeanUtils.copyProperties(app, projectInfo);

		rtlist.add(app);
		continue;
	    }

	    // 如果是当前用户处理的需求，直接展示
	    if (currentHandler.equals(loginName)) {
		ProjectInfoQueryResponse app = new ProjectInfoQueryResponse();
		AmsBeanUtils.copyProperties(app, projectInfo);

		rtlist.add(app);
		continue;
	    }

	}
	return rtlist;
    }

    /**
     * 
     * 方法功能描述： 保存需求信息
     * 
     * @param projectInfo
     * @return
     */
    public int saveProjectInfo(ProjectInfoQueryRequest projectInfoReq) {

	String currentHandler = projectInfoReq.getCurrentHandler();
	if (StringUtils.isEmpty(currentHandler)) {
	    // 设置当前处理人为当前用户
	    projectInfoReq.setCurrentHandler(DataBus.getLoginName());
	}

	String requireStatus = projectInfoReq.getStatus();
	if (StringUtils.isEmpty(requireStatus)) {
	    // 设置状态为新建
	    projectInfoReq.setStatus("0");
	}

	ProjectInfo projectInfo = new ProjectInfo();
	AmsBeanUtils.copyProperties(projectInfo, projectInfoReq);

	return projectInfoDAO.saveOrUpdateByPrimaryKey(projectInfo);
    }

    /**
     * 
     * 方法功能描述：删除需求信息
     * 
     * @param projectId
     * @return
     */
    public int deleteProjectInfo(ProjectInfoQueryRequest projectInfoReq) {
	ProjectInfo projectInfo = new ProjectInfo();
	AmsBeanUtils.copyProperties(projectInfo, projectInfoReq);
	return projectInfoDAO.deleteByPrimaryKey(projectInfo.getId());
    }

    /**
     * 
     * 方法功能描述：提交需求信息
     * 
     * @param projectInfo
     * @return
     */
    public int commitProjectInfo(ProjectInfoQueryRequest projectInfoReq) {

	int ret = RetCode.UNKNOWN_ERROR;

	if (null == projectInfoReq.getStatus()) {
	    ret = RetCode.UNKNOWN_STATUS;
	    return ret;
	}

	// 状态为新建，则启动流程
	if (projectInfoReq.getStatus().equals("0")) {
	    // key对应需求流转流程.bpmn文件中id的属性值
	    String processDefinitionKey = "需求流转流程";
	    // 启动流程，获得流程实例ID
	    String processId = businessworkManager.workFlowStart(
		    processDefinitionKey,
		    String.valueOf(projectInfoReq.getId()), null);
	    System.out.println("流程实例ID:" + processId);// 流程实例ID

	    // 更新ProjectInfo表该条记录的信息
	    ProjectInfo projectInfo = new ProjectInfo();
	    AmsBeanUtils.copyProperties(projectInfo, projectInfoReq);
	    ret = projectInfoDAO.saveOrUpdateByPrimaryKey(projectInfo);
	} else if (projectInfoReq.getStatus().equals("1")) {
	    //

	}

	if (RetCode.SUCC == ret) {
	    // 更新上一个处理人为当前处理人字段的值
	    projectInfoReq.setLastHandler(projectInfoReq.getCurrentHandler());
	    // 更新当前处理人字段的值为下一处理人字段的值
	    projectInfoReq.setCurrentHandler(projectInfoReq.getNextHandler());
	    // 更新下一处理人字段的值为空
	    projectInfoReq.setNextHandler(null);
	}

	return ret;

    }

}
